package View;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import Command.Command;
import Command.Select;
import Controller.MainController;
import Model.Connection;

public class CommanderClient extends Thread{

	private Socket cmdClient;
	private boolean isClose = false;;
	ArrayList<Command> _commandsSend;
	
	public CommanderClient(ArrayList<Command> commandsSend)
	{
		_commandsSend = commandsSend;
		start();
	}
	
	public void setCommands(ArrayList<Command> commandsSend)
	{
		this._commandsSend = commandsSend;
	}
	
	public void run()
	{
		try {
			cmdClient = Connection.socket;

			OutputStream os = cmdClient.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(os);
			
			InputStream is = cmdClient.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);
					
			
			do
			{
				synchronized (this) {
					this.wait();
				}
			
				//send the number of sqls and each sql's content
				int nCmdsSend = this._commandsSend.size();
				String[] sendInf = new String[nCmdsSend];
				int n = sendInf.length;
				
				for (int i = 0; i < n; i++)
				{
					Command cmd = _commandsSend.get(i);
					String str = cmd.getSql();
					sendInf[i] = str;
				}
				
				//Send nCmdsSend
				System.out.println("Send the number of commands: " + nCmdsSend);
				oos.writeInt(nCmdsSend);
				oos.flush();
				
				//Send the content of each command
				for(int i = 0; i <n; i++){
					oos.writeUTF(sendInf[i]);					
					oos.flush();
				}
				
				//receive the number of result and each sql's result
				int nResult;
				Vector<Object> res = new Vector<Object>();
				//receive nResult
				
				nResult = ois.readInt(); 
				System.out.println("Received the number of result " + nResult);
				Object obj;
				for(int i =0; i<nResult; i++)
				{
					obj = ois.readObject();
					res.add(obj);
					MainView mainView = (MainView) MainController.GetViewByName(MainController.MAIN_VIEW_NAME);					
					ImageIcon successIcon = new ImageIcon("img\\success.png");
					ImageIcon failedIcon = new ImageIcon("img\\failed.png");
					if(_commandsSend.get(i) instanceof Select
							&& !(obj instanceof String))
					{
						
						Object[] selRet = (Object[]) obj;
						if(selRet!= null && selRet.length == 2)
						{
							Vector<String> fields = (Vector<String>) selRet[0];
							Object[][] data = (Object[][]) selRet[1];
							System.out.println(data.length);
							JScrollPane table = mainView.createNewResultTable(fields.toArray(), data);
							mainView.AddItemToResult(table);
						}
					}
					else
					{
						JLabel retsult = new JLabel(String.valueOf(obj));
						if( (String.valueOf(obj)).contains("failed"))
							retsult.setIcon(failedIcon);
						else
							retsult.setIcon(successIcon);
							
						mainView.AddItemToResult(retsult);
					}
					
					
				}
			}while(!isClose);		
			
			ois.close();
			oos.close();
			
		} catch (IOException | InterruptedException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JPanel panel = null;							
			JOptionPane.showMessageDialog(panel, "Commander client Thread closed", 
				    "Error", JOptionPane.ERROR_MESSAGE);
		}
		
	}
}
